package com.tajiang.business.businesscount.wallet;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.http.Api;
import com.tajiang.business.common.utils.CommandTools;
import com.tajiang.business.common.utils.ToastUtils;

/**
 * 添加微信
 */
public class AddWeiXinActivity extends BaseActivity {

    private WebView mWebView;

    @Override
    protected void initTopBar() {
        setTitle("添加微信");
    }

    @Override
    protected void initLayout() {

    }

    @Override
    protected void initData() {

        try {
            mWebView = new WebView(this);
            setContentView(mWebView);
            mWebView.requestFocus();

            mWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int progress) {
                }
            });

            mWebView.setOnKeyListener(new View.OnKeyListener() { // webview can
                // go back
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK
                            && mWebView.canGoBack()) {
                        mWebView.goBack();
                        return true;
                    }
                    return false;
                }
            });
            mWebView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    if(!Api.weixin.equals(url)){
                        return true;
                    }
                    view.loadUrl(url);   //在当前的webview中跳转到新的url

                    return true;
                }
            });

            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setDefaultTextEncodingName("utf-8");

            mWebView.addJavascriptInterface(getHtmlObject(), "jsObj");
            mWebView.loadUrl(Api.weixin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Object getHtmlObject() {

        Object insertObj = new Object() {

            @JavascriptInterface
            public String HtmlcallJava() {

                if (CommandTools.isWeixinAvilible(AddWeiXinActivity.this)) {
                    Intent intent = new Intent();
                    PackageManager packageManager = getPackageManager();
                    intent = packageManager
                            .getLaunchIntentForPackage("com.tencent.mm");
//					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
//							| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
//							| Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else {
                    ToastUtils.showShort("您的手机暂无微信app");
                }

                return "Html call Java";
            }

            public String HtmlcallJava2(final String param) {
                return "Html call Java : " + param;
            }

            public void JavacallHtml() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    }
                });
            }

            public void JavacallHtml2() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    }
                });
            }
        };

        return insertObj;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_wei_xin);
    }
}
